package cn.dy.sbo.web;

import cn.dy.sbo.core.BusinessCatManager;
import cn.dy.sbo.core.FlowManager;
import cn.dy.sbo.core.FlowTypeManager;
import cn.dy.sbo.core.PartnerManager;
import cn.dy.sbo.model.BusinessCat;
import cn.dy.sbo.util.JsonUtil;
import cn.dy.sbo.vo.DropListVO;
import com.opensymphony.xwork2.ActionSupport;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class DroplistAction  extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private BusinessCatManager businessCatManager;
	
	@Autowired
	private FlowTypeManager flowTypeManager;
	
	@Autowired
	private FlowManager flowManager;
    @Autowired
    private PartnerManager partnerManager;

	private Long id;

    private String q;

    public String buscat() throws JsonGenerationException, JsonMappingException, IOException{
		List<BusinessCat> list = businessCatManager.getAll();
		
		Struts2Utils.renderJson(JsonUtil.objectToJson(list));
		return null;
	}
	
	public String flowtype() throws JsonGenerationException, JsonMappingException, IOException{
		List<DropListVO> list = flowTypeManager.getByCat(id);
		
		Struts2Utils.renderJson(JsonUtil.objectToJson(list));
		return null;
	}
	
	public String flow() throws JsonGenerationException, JsonMappingException, IOException{
		List<DropListVO> list = flowManager.getByCat(id);
		
		Struts2Utils.renderJson(JsonUtil.objectToJson(list));
		return null;
	}
	
	public String getLocal(){
		Object obj = Struts2Utils.getSession().getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME);
		if(obj == null)
			return "zh_CN";
		return ((Locale)obj).getLanguage();
	}

    public String partnercode()throws IOException{
        q = new String(q.getBytes("iso-8859-1"), "utf8");
        try{
        List<DropListVO> list = partnerManager.findByCode(q);
        Struts2Utils.renderJson(JsonUtil.objectToJson(list));
        }catch(Exception e)  {
              e.printStackTrace();
        }
        return null;
    }

    public String partner()throws IOException{
        q = new String(q.getBytes("iso-8859-1"), "utf8");
        try{
        List<DropListVO> list = partnerManager.findByName(q);
        Struts2Utils.renderJson(JsonUtil.objectToJson(list));
        }catch(Exception e)  {
              e.printStackTrace();
        }
        return null;
    }

    public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

    public void setQ(String cusName) {
        this.q = cusName;
    }
}
